<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" %>
<c:if test="${empty sessionScope.user}">
    <c:redirect url="/login"/>
</c:if>
<c:if test="${sessionScope.role==2}">
    <script>
        alert("你没有权限访问该页面")
    </script>
</c:if>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>评论管理 - 新闻管理系统</title>
    <link rel="stylesheet" href="<c:url value="/resources/css/admin-style.css"/>"/>
    <style>
        .search {
            height: 60px;
        }
    </style>
</head>
<body class="admin-body">
<!-- 侧边栏 -->
<aside class="admin-sidebar">
    <div class="sidebar-header">
        <h2>新闻管理系统</h2>
    </div>
    <nav class="sidebar-nav">
        <a href="<c:url value="/dashboard"/>" class="nav-item">仪表盘</a>
        <a href="<c:url value="/list/news"/>" class="nav-item">新闻管理</a>
        <a href="<c:url value="/list/category"/>" class="nav-item">类型管理</a>
        <c:if test="${sessionScope.role!=3}">
            <a href="<c:url value="/list/user"/>" class="nav-item">用户管理</a>
        </c:if>
        <a href="<c:url value="/list/comm"/>" class="nav-item active">评论管理</a>
        <a href="<c:url value="/logout"/>" class="nav-item logout-item">退出登录</a>
    </nav>
</aside>

<!-- 主内容区 -->
<main class="admin-main">
    <header class="admin-header">
        <h1>评论管理</h1>
        <button id="batch-delete-btn" class="delete-btn" onclick="batchDelete()">批量删除</button>
    </header>

    <!-- 搜索栏 -->
    <div class="search">
        <input type="text" id="comment-search" placeholder="搜索评论内容/用户名...">
        <button id="comment-search-btn" class="search-btn">搜索</button>
    </div>

    <!-- 评论表格 -->
    <table class="comment-table" id="comment-list-table">
        <thead>
        <tr>
            <th>选项</th>
            <th>ID</th>
            <th>评论内容</th>
            <th>所属新闻</th>
            <th>评论用户</th>
            <th>评论时间</th>
            <th>状态</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        <!-- JS动态渲染 -->
        <c:forEach items="${commList}" var="comm">
            <tr>
                <td><input type="checkbox" value="${comm.id}" class="user-chack"></td>
                <td>${commList.indexOf(comm)+1}</td>
                <td>${comm.comment}</td>
                <td>${comm.news}</td>
                <td>${comm.comment_user}</td>
                <td>${comm.update_time}</td>
                <td><c:if test="${comm.status==1}">已审核</c:if><c:if test="${comm.status==0}">未审核</c:if></td>
                <td>
                    <c:if test="${comm.status==1}">
                        <button class="delete-btn" onclick="checkComm('${comm.id}','0')">撤销审核</button>
                    </c:if>
                    <c:if test="${comm.status==0}">
                        <button class="save-btn" onclick="checkComm('${comm.id}','1')">审核</button>
                    </c:if>
                </td>
            </tr>
        </c:forEach>
        </tbody>
    </table>
</main>
<script src="<c:url value="/resources/js/jquery-3.7.1.min.js"/> "></script>
<script>
    function checkComm(id, status) {
        $(document).ready(function () {
            $.ajax({
                url: "/list/alter/check",
                method: 'POST',
                data: {
                    id: id,
                    status: status
                },
                success: function (data) {
                    console.log(data);
                    console.log("操作成功！");
                    window.location.reload();
                },
                error: function (data) {
                    console.log("错误！");
                }
            });
        });
    }

    function batchDelete() {
        var idList = [];
        $("input.user-chack:checked").each(function () {
            idList.push($(this).val());
        });
        if (confirm("确认删除？")) {
            $.ajax({
                url: '/list/del/comment',
                method: 'POST',
                traditional: true,
                data: {
                    ids: idList
                },
                success: function (data) {
                    if (data === 0 || data < idList.length) {
                        alert("评论中有已经审核的评论，该部分无法删除，请禁用后重试！");
                        window.location.reload();
                        return;
                    }
                    alert("删除成功！");
                    window.location.reload();
                }
            });
        }
    }
</script>
</body>
</html>